package com.neiquan.backstage.user.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.pub.bsc.dao.po.User;
import com.neiquan.backstage.user.UserService;

@Service
public class UserServiceImpl implements UserService {
	private static Class<?> poc = User.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;

	@Override
	public User queryByPhone(String phone, Integer memberId) {
		List<User> entityUs = (List<User>) extraSpringHibernateTemplate
				.getHibernateTemplate().find("From User where phone=? and memberId=?", phone,memberId);
		if(entityUs.size()>0){
			User user = entityUs.get(0);
			return user;
		}
		return null;
	}
	
	@Override
	public User queryByName(String name, Integer memberId) {
		List<User> entityUs = (List<User>) extraSpringHibernateTemplate
				.getHibernateTemplate().find("From User where name=? and memberId=?", name,memberId);
		if(entityUs.size()>0){
			User user = entityUs.get(0);
			return user;
		}
		return null;
	}
	
	@Override
	public User queryById(Integer id) {
		return extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "userId", id);
	}

	@Override
	public List<User>queryByentreprEneurName(String entrepreneurName, int memberId) {
		// TODO Auto-generated method stub
		return (List<User>) extraSpringHibernateTemplate.getHibernateTemplate().find("From User where entrepreneurName=? and memberId=? and type!=1",entrepreneurName, memberId);
	}

	@Override
	public User queryByopendId(String openId, Integer memberId) {
		// TODO Auto-generated method stub
		List<User> users=(List<User>) extraSpringHibernateTemplate.getHibernateTemplate().find("From User where weixin_openid=? and memberId=?", openId,memberId);
		if(users.size()>0){
			User user = users.get(0);
			return user;
		}
		return null;
				
	}

	@Override
	public List<User> queryByStatus(Integer memberId,String entrepreneurUniqueId) {
		// TODO Auto-generated method stub
		return (List<User>) extraSpringHibernateTemplate.getHibernateTemplate().find("From User where type=? and memberId=? and entrepreneurUniqueId=?",1, memberId,entrepreneurUniqueId);
	}

	@Override
	public List<User> queryEmplByStatus(Integer memberId, String entrepreneurUniqueId) {
		// TODO Auto-generated method stub
		return (List<User>) extraSpringHibernateTemplate.getHibernateTemplate().find("From User where type!=? and memberId=? and entrepreneurUniqueId=?",1, memberId,entrepreneurUniqueId);
	}
	
}
